package com.manster.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manster.entity.SysMenu;
import com.manster.entity.SysRoleMenu;
import com.manster.entity.SysUser;
import com.manster.util.R;
import com.manster.vo.SysMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author manster
 * @since 2022-03-13
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @ApiOperation("获取用户菜单")
    @GetMapping("/nav")
    public R nav(Principal principal){

        SysUser user = userService.getByUsername(principal.getName());

        //获取权限信息
        String authorityInfo = userService.getUserAuthorityInfo(user.getId());
        String[] authorityInfoArray = StringUtils.tokenizeToStringArray(authorityInfo, ",");

        //获取导航栏
        List<SysMenuVo> nav = menuService.getCurrentUserNav();

        return R.success().data(MapUtil.builder()
                .put("authoritys",authorityInfoArray)
                .put("nav", nav)
                .map()
        );
    }

    @ApiOperation("获取菜单列表")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @GetMapping("/list")
    public R list(){
        List<SysMenu> menus = menuService.treeMenu();
        return R.success().data(menus);
    }

    @ApiOperation("获取菜单详情")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @GetMapping("/info/{id}")
    public R info(@PathVariable(name = "id") Long id){
        return R.success().data(menuService.getById(id));
    }

    @ApiOperation("新增菜单")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody SysMenu sysMenu){
        menuService.save(sysMenu);
        return R.success().data(sysMenu);
    }

    @ApiOperation("修改菜单")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    @PostMapping("/update")
    public R update(@Validated @RequestBody SysMenu sysMenu){
        menuService.updateById(sysMenu);
        //清除所有与该菜单相关缓存信息
        userService.clearUserAuthorityInfoByMenuId(sysMenu.getId());
        return R.success().data(sysMenu);
    }

    @ApiOperation("删除菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){

        //有子菜单则删除失败
        int count = menuService.count(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if(count > 0){
            return R.fail().msg("请先删除子菜单");
        }
        //清除缓存再删除菜单
        userService.clearUserAuthorityInfoByMenuId(id);
        menuService.removeById(id);
        //同步删除中间关联表
        roleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("menu_id",id));
        return R.success();
    }

}
